from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Date
from sqlalchemy.orm import relationship
from datetime import datetime
from config.database import Base

class Salary(Base):
    """薪资表"""
    __tablename__ = 'sys_salary'

    salary_id = Column(Integer, primary_key=True, autoincrement=True, comment='薪资ID')
    employee_id = Column(Integer, ForeignKey('employee_info.id'), nullable=False, comment='员工ID')
    month = Column(Integer, nullable=False, comment='月份')
    year = Column(Integer, nullable=False, comment='年份')
    daily_salary = Column(Float, nullable=False, comment='日薪')
    attendance_days = Column(Float, nullable=False, comment='出勤天数')
    gross_salary = Column(Float, nullable=False, comment='应发工资')
    total_payment = Column(Float, nullable=False, comment='实发工资')
    social_security = Column(Float, nullable=False, comment='社保金额')
    net_salary = Column(Float, nullable=False, comment='实发工资')
    create_time = Column(DateTime, default=datetime.now, comment='创建时间')
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    remark = Column(String(500), comment='备注')

    # 建立与员工表的关联
    employee = relationship('EmployeeInfo', back_populates='salaries')

    def to_dict(self):
        """转换为字典"""
        return {
            'salaryId': self.salary_id,
            'employeeId': self.employee_id,
            'employeeName': self.employee.name if self.employee else None,
            'month': self.month,
            'year': self.year,
            'dailySalary': self.daily_salary,
            'attendanceDays': self.attendance_days,
            'grossSalary': self.gross_salary,
            'totalPayment': self.total_payment,
            'socialSecurity': self.social_security,
            'netSalary': self.net_salary,
            'createTime': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'updateTime': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None,
            'remark': self.remark
        } 